package com.hjm.project.terminal.model.controller;

import java.util.List;
import java.util.Map;

import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.terminal.manufacturer.service.IManufacturerService;
import com.hjm.project.terminal.model.domain.TerminalModel;
import com.hjm.project.terminal.model.service.ITerminalModelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

/**
 * 终端型号Controller
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/terminal/model")
public class TerminalModelController extends BaseController
{
    private String prefix = "terminal/model";

    @Autowired
    private ITerminalModelService terminalModelService;

    @Autowired
    private IManufacturerService manufacturerService;

    @RequiresPermissions("terminal:model:view")
    @GetMapping()
    public String model(ModelMap mmap) throws Exception {
        List<Map> manufacturer = manufacturerService.getManufacturer(ShiroUtils.isAgent() ? ShiroUtils.getLoginAgentNum() : null);
        mmap.put("manufacturer",manufacturer);
        return prefix + "/model";
    }

    /**
     * 查询终端型号列表
     */
    @RequiresPermissions("terminal:model:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TerminalModel terminalModel) throws Exception {
        startPage();
        List<TerminalModel> list = terminalModelService.selectTerminalModelList(terminalModel);
        return getDataTable(list);
    }

    /**
     * 导出终端型号列表
     */
    @RequiresPermissions("terminal:model:export")
    @Log(title = "终端型号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TerminalModel terminalModel)
    {
        List<TerminalModel> list = terminalModelService.selectTerminalModelList(terminalModel);
        ExcelUtil<TerminalModel> util = new ExcelUtil<TerminalModel>(TerminalModel.class);
        return util.exportExcel(list, "model");
    }

    /**
     * 新增终端型号
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存终端型号
     */
    @RequiresPermissions("terminal:model:add")
    @Log(title = "终端型号", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TerminalModel terminalModel)
    {
        return toAjax(terminalModelService.insertTerminalModel(terminalModel));
    }

    /**
     * 修改终端型号
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TerminalModel terminalModel = terminalModelService.selectTerminalModelById(id);
        mmap.put("terminalModel", terminalModel);
        return prefix + "/edit";
    }

    /**
     * 修改保存终端型号
     */
    @RequiresPermissions("terminal:model:edit")
    @Log(title = "终端型号", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TerminalModel terminalModel)
    {
        return toAjax(terminalModelService.updateTerminalModel(terminalModel));
    }

    /**
     * 删除终端型号
     */
    @RequiresPermissions("terminal:model:remove")
    @Log(title = "终端型号", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(terminalModelService.deleteTerminalModelByIds(ids));
    }

    /**
     * 选择型号
     */
    @GetMapping("/selectModel")
    public String selectModel(ModelMap map)
    {
        return prefix + "/selectModel";
    }
}
